/*
Content: Figures E.1 and E.2, Table A.2
Paper: "Immigrants Return Intentions and Labor Market Behavior when the Home Country is Unsafe"
Authors: Jacopo Bassetto, Teresa Freitas-Monteiro
*/

		
********************************************************************************
*** LOAD ANALYTICAL DATASET
********************************************************************************

use "$data/finaldata_terrorsoep_light_final.dta", clear


********************************************************************************
*** Figure E.1: JOINT BALANCE TEST
********************************************************************************

eststo clear
foreach event in terror     {	
foreach var in	m5y m4y m3y {
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distance`event'_`var'_p90,-3,3) & distance`event'_`var'_p90!= 0"
global controls "i.soep_corigin_id#i.syear i.syear#i.month" 	

* Regression for joint significance of covariates relative to the prob of being interviewed right after an event
qui eststo `event'`var': reghdfe post`event'_`var'_p90 i.sex c.age c.ysm i.maritstat i.educ_aftm2 i.child i.bula if $select_reg_M  & sample_`event'_`var'==1 , vce(cluster clusterterror_`var'_p90) absorb($controls)

cap gen sample_reg_`event'_`var' = 1 if e(sample) == 1
replace  sample_reg_`event'_`var' = 0 if sample_reg_`event'_`var' == . 

* Create coefplot of estimated coefficients
coefplot `event'`var', keep(*.sex age ysm *.maritstat *.educ_aftm2 *.child)  horizontal omitted graphregion(color(white)) ///
xline(0, lcolor(cranberry)) rename(2.sex = "Female" age = "Age" 1.maritstat = "Married" 1.educ_aftm2 = "Upper sec. edu." 2.educ_aftm2 = "Post sec. edu." 3.educ_aftm2 = "Higher edu." 1.child= "Has child") xscale(r(-0.04(0.02)0.04)) xlabel(-0.04(0.02)0.04, nogrid) ylabel(, nogrid) ciopts(color(navy)) mcolor(navy) xsize(5.5) ysize(4)

			graph save "Graph" "$graph/covbalance_soep_`event'_`var'_final.gph", replace
			graph export "$graph/covbalance_soep_`event'_`var'_final.pdf", as(pdf) replace		
			graph export "$graph/covbalance_soep_`event'_`var'_final.eps", as(eps) replace		

}
}

		
	

********************************************************************************
*** Figure E.2: DISTRIBUTION OF INTERVIEWS AROUND EVENTS
********************************************************************************


* 1) Generate the total number of interviews in each month-year-country combinations

foreach event in terror     {	
foreach var in	m5y m4y m3y  {
cap drop  tot_d`event'_`var'_p90 
cap drop first_d`event'_`var'_p90 
cap drop  tot_cd`event'_`var'_p90 
cap drop  first_cd`event'_`var'_p90
cap drop  tot_cde`event'_`var'_p90 
cap drop  first_cde`event'_`var'_p90
* Sum by distance to event, sum number of interviews
bys distance`event'_`var'_p90 : gen tot_d`event'_`var'_p90  = _N
bys distance`event'_`var'_p90 : gen first_d`event'_`var'_p90  = _n == 1
		
* Sum by country of origin and distance to event, sum number of interviews
bys soep_corigin_id  distance`event'_`var'_p90 relevant`event'_`var'_p90 : gen tot_cde`event'_`var'_p90  = _N   if distance`event'_`var'_p90!=.
bys soep_corigin_id  distance`event'_`var'_p90 relevant`event'_`var'_p90 : gen first_cde`event'_`var'_p90  = _n == 1   if distance`event'_`var'_p90!=.
}
}
 
foreach event in terror     {	
foreach var in	m5y m4y m3y  {
	cap drop ratio_cd* ratio_cde* sum_cd*
	gen sum_d`event'_`var'_p90   = _N if distance`event'_`var'_p90!=.
	bys soep_corigin_id relevant`event'_`var'_p90 : gen sum_cde`event'_`var'_p90  = _N  if distance`event'_`var'_p90!=.
	g ratio_d`event'_`var'_p90= tot_d`event'_`var'_p90/sum_d`event'_`var'_p90 if distance`event'_`var'_p90!=.
	g ratio_cde`event'_`var'_p90= tot_cde`event'_`var'_p90/sum_cde`event'_`var'_p90 if distance`event'_`var'_p90!=.
	
}
}
	
	
* 2) Graph tot interviews around events

* Sample being used but including 0 e.g. time of the interview	
	foreach event in terror     {	
foreach var in	 m3y {
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distance`event'_`var'_p90,-3,3) & ratiointer`event'_m3y_p90!=1"
global controls "i.soep_corigin_id i.soep_corigin_id#i.syear i.month#i.syear i.bula" 	
reghdfe remain_ger_per post`event'_`var'_p90  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M , vce(cluster clusterterror_`var'_p90) absorb($controls)
cap drop  tempsample`event'_`var'
g tempsample`event'_`var'=e(sample)		
}
}


foreach event in terror     {	
foreach var in	 m3y  {
global select_reg_M "!inlist(soep_corigin_id,.,1) & ratiointer`event'_`var'_p90!=1"
graph box ratio_cde`event'_`var'_p90  if $select_reg_M  & first_cde`event'_`var'_p90  == 1, over(distance`event'_`var'_p90) graphregion(color(white)) ytitle("Share of survey respondents", margin(medsmall) size(medsmall))  b1title("Distance (in months) from event", margin(medsmall) size(medsmall)) box(1, color(navy))  marker(1 , mcolor(navy))
}
}	

graph save "Graph" "$graph/interviews_bydistancecorigin_box_final.gph", replace
graph export "$graph/interviews_bydistancecorigin_box_final.pdf", as(pdf) name("Graph") replace
	   

		
clear 
	   




	